<template>
<div id="pmy1">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="昵称" prop="name" style="width: 100%;" >
    <el-input v-model="ruleForm.userName" ></el-input>
  </el-form-item>
  <el-form-item label="性别" prop="region">
    <el-select v-model="ruleForm.sex" style="width: 20%;" placeholder="">
      <el-option label="女" value="女"></el-option>
      <el-option label="男" value="男"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="生日" required>
    <el-col :span="11">
      <el-form-item prop="date1">
        <el-date-picker type="date" placeholder="生日" v-model="ruleForm.birthday" style="width: 100%;"></el-date-picker>
      </el-form-item>
    </el-col>
  </el-form-item>
  <el-form-item label="地址" prop="desc">
    <el-input type="textarea" v-model="ruleForm.address"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</div>
</template>
<script>
  export default {
     
    data: function() {
      return {
        ruleForm: {
          
          userName: '',
          sex: '',
          birthday: '',
          // email:'',
          address: ''
        },
        rules: {
          userName: [
            { required: true, message: '请输入昵称', trigger: 'blur' },
            { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别', trigger: 'change' }
          ],
          birthday: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          address: [
            { required: true, message: '请填写地址', trigger: 'blur' }
          ]
        }
      };
    },
    created(){
    this.$axios.get("http://localhost:3000/users/user")
    .then(res => {
      console.log("查询结果是：");
      console.log(res.data.data);
      this.ruleForm=res.data.data[0]
      console.log(this.ruleForm)
      // console.log(user.userName);
    })
    .catch(err => {
      console.log("错误信息"+err)
    })
  },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            
          alert("submit!");
          // console.log(this.)
          this.$axios.post("http://localhost:3000/users/changeuser", this.ruleForm)
            .then(res => {
              console.log("用户信息更新成功！", res);
              this.$router.push("/index/person/pmyo"); //路由转向主页组件
            })
            .catch(err => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style>
#pmy1{
 width: 500px;
  height: 380px;
  position:absolute;
  left:25%;
  top:30%;
  margin-left:100px;
  margin-top:-40px;
  border-radius: 5px;
  /* background-color: #ff0; */
  padding:20px 50px 10px 10px;
}
</style>